package com.oa.ylms.apps.business.leave.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.leave.ILeaveCheckService;
import com.oa.ylms.apps.business.leave.ILeaveInfoService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.business.plan.IMessageInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.ICheckStates;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.dao.leave.ILeaveCheckDao;
import com.oa.ylms.apps.dao.leave.ILeaveInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.leave.LeaveCheckValue;
import com.oa.ylms.apps.entity.leave.LeaveInfoValue;

public class LeaveCheckServiceImpl implements ILeaveCheckService {

	private ILeaveCheckDao dao;
	private ILeaveInfoDao leaveInfoDao;
	private IStaffInfoService staffInfoService;
	private ILeaveInfoService leaveInfoService;
	private ICheckPlanService checkPlanService;
	private LeaveInfoValue leaveInfo;
	private ArchiveInfoValue archiveInfo;
	private LeaveCheckValue leaveCheck;
	private IMessageInfoService messageService;

	public List<LeaveCheckValue> queryLeaveCheckByLeaveId(int leaveId)
			throws Exception {
		return dao.queryLeaveCheckByLeaveId(leaveId);
	}

	public int countTpagesByCheckList(StaffInfoValue staffInfo,
			String staffName, int rows) throws Exception {
		if ( staffInfo.getArchiveInfo().getPostInfo().getPostCategory().getCategoryId()==1){
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
			archiveInfo.setStates(IDEDStates.ENABLE);
		}
		leaveInfo.setStates(sectionId);
		return dao.countTpagesByCheckList(leaveInfo, archiveInfo, rows);
		}
		return 0;
	}

	public int getTotalNumber() throws Exception {

		
		return dao.getTotalNumber();
		
		
	}

	public List<LeaveInfoValue> queryCheckList(StaffInfoValue staffInfo,
			String staffName, int rows, int pageNo) throws Exception {
		if ( staffInfo.getArchiveInfo().getPostInfo().getPostCategory().getCategoryId()==1){
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		if (StringUtils.isNotEmpty(staffName)
				&& StringUtils.isNotBlank(staffName)) {
			archiveInfo.setStaffName(staffName);
			archiveInfo.setStates(IDEDStates.ENABLE);
		}
		leaveInfo.setStates(sectionId);
		return dao.queryCheckList(leaveInfo, archiveInfo, rows, pageNo);
		}
		return null;
	}

	public void checkFailed(StaffInfoValue staffInfo, int leaveId,
			String comments) throws Exception {
		leaveInfo = leaveInfoService.queryLeaveInfoByLeaveId(leaveId);
		int currentState = leaveInfo.getStates();
		leaveInfo.setStates(IOrderStates.FAILED);
		messageService.addMessageInfo("《"+leaveInfo.getTitle()+"》的请假单审核结果","您的请假单标题为:"+leaveInfo.getTitle()+",未通过审核。", leaveInfo.getStaffInfo().getStaffId());
		leaveInfoDao.modifyLeaveInfo(leaveInfo);
		leaveCheck.setStaffInfo(staffInfo);
		leaveCheck.setComments(comments);
		leaveCheck.setIsPassed(ICheckStates.CHECKFAILED);
		leaveCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		leaveCheck.setLeaveInfo(leaveInfo);
		leaveCheck.setCheckOrder(currentState);
		dao.add(leaveCheck);
	}

	public void checkPassed(StaffInfoValue staffInfo, int leaveId,
			String comments) throws Exception {
		leaveInfo = leaveInfoService.queryLeaveInfoByLeaveId(leaveId);
		String planOrders = leaveInfo.getCheckPlan().getPlanOrders();
		int currentState = leaveInfo.getStates();
		int nextState = checkPlanService.getStates(planOrders, currentState);
		if(nextState == -2){
			messageService.addMessageInfo("《"+leaveInfo.getTitle()+"》的请假单审核结果","您的请假单标题为:"+leaveInfo.getTitle()+",通过审核。", leaveInfo.getStaffInfo().getStaffId());
		}
		leaveInfo.setStates(nextState);
		leaveInfoDao.modifyLeaveInfo(leaveInfo);
		leaveCheck.setStaffInfo(staffInfo);
		leaveCheck.setComments(comments);
		leaveCheck.setIsPassed(ICheckStates.CHECKPASSED);
		leaveCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		leaveCheck.setLeaveInfo(leaveInfo);
		leaveCheck.setCheckOrder(currentState);
		dao.add(leaveCheck);
	}

	public ILeaveCheckDao getDao() {
		return dao;
	}

	public void setDao(ILeaveCheckDao dao) {
		this.dao = dao;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public LeaveInfoValue getLeaveInfo() {
		return leaveInfo;
	}

	public void setLeaveInfo(LeaveInfoValue leaveInfo) {
		this.leaveInfo = leaveInfo;
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public ILeaveInfoService getLeaveInfoService() {
		return leaveInfoService;
	}

	public void setLeaveInfoService(ILeaveInfoService leaveInfoService) {
		this.leaveInfoService = leaveInfoService;
	}

	public ILeaveInfoDao getLeaveInfoDao() {
		return leaveInfoDao;
	}

	public void setLeaveInfoDao(ILeaveInfoDao leaveInfoDao) {
		this.leaveInfoDao = leaveInfoDao;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

	public LeaveCheckValue getLeaveCheck() {
		return leaveCheck;
	}

	public void setLeaveCheck(LeaveCheckValue leaveCheck) {
		this.leaveCheck = leaveCheck;
	}

	public void setMessageService(IMessageInfoService messageService) {
		this.messageService = messageService;
	}

	public IMessageInfoService getMessageService() {
		return messageService;
	}

}
